package com.sk.waste.app.utils

import android.content.Context
import android.os.Environment
import android.util.Log
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCBaseUtil
import com.lc.smc.lib.utils.LCSPUtil
import com.sk.waste.app.application.MyApplication
import com.sk.waste.app.bases.BaseActiivity
import com.sk.waste.app.config.AppConfig
import com.sk.waste.app.models.UserModel
import okhttp3.Call
import okhttp3.Callback
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.io.IOException
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Scanner


/**
 *  @time 2024/7/12  8:44
 *  @author Im_jingwei
 *  @desc 登录工具
 */
object LoginUtil {

    /**
     *  @time 2024/8/30  9:34
     *  @author Im_jingwei
     *  @desc 创建bufferedWriter
     */
    fun init() {
    }

    /**
     *  @time 2024/5/7  11:29
     *  @desc 用户登录
     *  @param url 登录地址
     *  @param params 报文
     *  @param type 0 自动登录，1 手动登录
     */
    fun loginSubmit(type: Int, activity: BaseActiivity, url: String, params: String, callback: LCInterface.LcCallBack?) {
        if (type != 0) {
            activity.showProgress()
        }
        val httpUtil = OkHttpUtil()
        httpUtil.httpRequest(url, params, object : LCInterface.LcCallBackListener {
            override fun onFailure(call: Call, e: IOException) {
                if (type != 0) {
                    activity.cancleProgress()
                    LCBaseUtil.showToast(activity, "登录失败，" + e.message)
                }
            }

            @Throws(IOException::class)
            override fun onSuccess(call: Call, response: String) {
                try {
                    if (type != 0) {
                        activity.cancleProgress()
                    }
                    val jsonObject = JSONObject(response)
                    val success = jsonObject.getBoolean("success")
                    val msg = jsonObject.getString("message")
                    val code = jsonObject.getInt("code")
                    if (success) {
                        val data = jsonObject.getJSONObject("data")
                        saveLoginInfo(activity, data)
                        if (type != 0) {
                            activity.showToast("登录成功！")
                        }
//                        toMain()
                        callback?.callBack()
//                        LCLogger.d(TAG, "登录成功！,登录信息：$jsonObject")
                    } else {
                        if (type != 0) {
                            LCBaseUtil.showToast(activity, "登录失败，$msg")
                        }
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    if (type != 0) {
                        activity.cancleProgress()
                        LCBaseUtil.showToast(activity, "登录失败，" + e.message)
                    }
                }
            }
        })
    }


    /**
     *  @time 2024/7/8  10:12
     *  @author Im_jingwei
     *  @desc 保存登录状态和登录用户名
     */
    @Throws(JSONException::class)
    fun saveLoginInfo(mContext: Context, json: JSONObject) {
        val userJson = json.getJSONObject("user")
        val userInfo = UserModel().apply { getModelInfo(userJson) }
        MyApplication.instance.setUserInfo(userInfo)
        LCSPUtil.save(mContext, AppConfig.SP_ACCESS_TOKEN, json.getString("token"))
        LCSPUtil.save(mContext, AppConfig.SP_USER_ACCOUNT, userJson.getString("account"))
        LCSPUtil.save(mContext, AppConfig.SP_USER_ID, userJson.getString("userId"))
        LCSPUtil.save(mContext, AppConfig.SP_USER_AVATAR, userJson.getString("avatar"))
        LCSPUtil.save(mContext, AppConfig.SP_USER_PHONE, userJson.getString("phone"))
    }


}